package utils

import "strings"

// EPPStatus 表示一个EPP状态
type EPPStatus struct {
	Code        string `json:"code"`
	Name        string `json:"name"`
	Description string `json:"description"`
	NameCN      string `json:"name_cn"`
	DescCN      string `json:"desc_cn"`
	Type        string `json:"type"` // client, server, or other
}

// EPPStatusMap 标准EPP状态码映射
var EPPStatusMap = map[string]*EPPStatus{
	// 基本状态
	"ok": {
		Code:        "ok",
		Name:        "OK",
		Description: "This is the standard status for a domain, meaning it has no pending operations or prohibitions",
		NameCN:      "正常",
		DescCN:      "域名状态正常，没有待处理的操作或限制",
		Type:        "other",
	},
	"inactive": {
		Code:        "inactive",
		Name:        "Inactive",
		Description: "Domain is not activated in DNS",
		NameCN:      "未激活",
		DescCN:      "域名在DNS中未激活",
		Type:        "other",
	},

	// Client状态（注册商设置）
	"clienthold": {
		Code:        "clienthold",
		Name:        "Client Hold",
		Description: "Domain is not activated in DNS by registrar",
		NameCN:      "注册商暂停",
		DescCN:      "注册商暂停了域名的DNS解析",
		Type:        "client",
	},
	"clientdeleteprohibited": {
		Code:        "clientdeleteprohibited",
		Name:        "Client Delete Prohibited",
		Description: "Registrar has locked domain to prevent deletion",
		NameCN:      "注册商禁止删除",
		DescCN:      "注册商锁定域名防止删除",
		Type:        "client",
	},
	"clienttransferprohibited": {
		Code:        "clienttransferprohibited",
		Name:        "Client Transfer Prohibited",
		Description: "Registrar has locked domain to prevent transfer",
		NameCN:      "注册商禁止转移",
		DescCN:      "注册商锁定域名防止转移",
		Type:        "client",
	},
	"clientupdateprohibited": {
		Code:        "clientupdateprohibited",
		Name:        "Client Update Prohibited",
		Description: "Registrar has locked domain to prevent updates",
		NameCN:      "注册商禁止更新",
		DescCN:      "注册商锁定域名防止更新",
		Type:        "client",
	},
	"clientrenewprohibited": {
		Code:        "clientrenewprohibited",
		Name:        "Client Renew Prohibited",
		Description: "Registrar has locked domain to prevent renewal",
		NameCN:      "注册商禁止续费",
		DescCN:      "注册商锁定域名防止续费",
		Type:        "client",
	},

	// Server状态（注册局设置）
	"serverhold": {
		Code:        "serverhold",
		Name:        "Server Hold",
		Description: "Domain is not activated in DNS by registry",
		NameCN:      "注册局暂停",
		DescCN:      "注册局暂停了域名的DNS解析",
		Type:        "server",
	},
	"serverdeleteprohibited": {
		Code:        "serverdeleteprohibited",
		Name:        "Server Delete Prohibited",
		Description: "Registry has locked domain to prevent deletion",
		NameCN:      "注册局禁止删除",
		DescCN:      "注册局锁定域名防止删除",
		Type:        "server",
	},
	"servertransferprohibited": {
		Code:        "servertransferprohibited",
		Name:        "Server Transfer Prohibited",
		Description: "Registry has locked domain to prevent transfer",
		NameCN:      "注册局禁止转移",
		DescCN:      "注册局锁定域名防止转移",
		Type:        "server",
	},
	"serverupdateprohibited": {
		Code:        "serverupdateprohibited",
		Name:        "Server Update Prohibited",
		Description: "Registry has locked domain to prevent updates",
		NameCN:      "注册局禁止更新",
		DescCN:      "注册局锁定域名防止更新",
		Type:        "server",
	},
	"serverrenewprohibited": {
		Code:        "serverrenewprohibited",
		Name:        "Server Renew Prohibited",
		Description: "Registry has locked domain to prevent renewal",
		NameCN:      "注册局禁止续费",
		DescCN:      "注册局锁定域名防止续费",
		Type:        "server",
	},

	// Pending状态
	"pendingcreate": {
		Code:        "pendingcreate",
		Name:        "Pending Create",
		Description: "Domain creation is pending",
		NameCN:      "待创建",
		DescCN:      "域名创建待处理",
		Type:        "other",
	},
	"pendingdelete": {
		Code:        "pendingdelete",
		Name:        "Pending Delete",
		Description: "Domain is scheduled for deletion",
		NameCN:      "待删除",
		DescCN:      "域名已计划删除",
		Type:        "other",
	},
	"pendingrenew": {
		Code:        "pendingrenew",
		Name:        "Pending Renew",
		Description: "Domain renewal is pending",
		NameCN:      "待续费",
		DescCN:      "域名续费待处理",
		Type:        "other",
	},
	"pendingrestore": {
		Code:        "pendingrestore",
		Name:        "Pending Restore",
		Description: "Domain restore is pending",
		NameCN:      "待恢复",
		DescCN:      "域名恢复待处理",
		Type:        "other",
	},
	"pendingtransfer": {
		Code:        "pendingtransfer",
		Name:        "Pending Transfer",
		Description: "Domain transfer is pending",
		NameCN:      "待转移",
		DescCN:      "域名转移待处理",
		Type:        "other",
	},
	"pendingupdate": {
		Code:        "pendingupdate",
		Name:        "Pending Update",
		Description: "Domain update is pending",
		NameCN:      "待更新",
		DescCN:      "域名更新待处理",
		Type:        "other",
	},

	// 生命周期状态
	"addperiod": {
		Code:        "addperiod",
		Name:        "Add Period",
		Description: "Domain is in the add grace period after initial registration",
		NameCN:      "新增宽限期",
		DescCN:      "域名在初始注册后的新增宽限期内",
		Type:        "other",
	},
	"autorenewperiod": {
		Code:        "autorenewperiod",
		Name:        "Auto-Renew Period",
		Description: "Domain is in auto-renewal grace period",
		NameCN:      "自动续费期",
		DescCN:      "域名在自动续费宽限期内",
		Type:        "other",
	},
	"renewperiod": {
		Code:        "renewperiod",
		Name:        "Renew Period",
		Description: "Domain is in renewal grace period",
		NameCN:      "续费宽限期",
		DescCN:      "域名在续费宽限期内",
		Type:        "other",
	},
	"redemptionperiod": {
		Code:        "redemptionperiod",
		Name:        "Redemption Period",
		Description: "Domain is in redemption period after deletion",
		NameCN:      "赎回期",
		DescCN:      "域名在删除后的赎回期内",
		Type:        "other",
	},
	"transferperiod": {
		Code:        "transferperiod",
		Name:        "Transfer Period",
		Description: "Domain is in transfer grace period",
		NameCN:      "转移期",
		DescCN:      "域名在转移宽限期内",
		Type:        "other",
	},
}

// NormalizeEPPStatus 标准化EPP状态码
func NormalizeEPPStatus(status string) string {
	// 清理状态字符串
	status = cleanStatusString(status)
	
	// 检查是否是有效的EPP状态
	if _, ok := EPPStatusMap[status]; ok {
		return status
	}
	
	// 处理变体（如auto-renewperiod vs autorenewperiod）
	status = normalizeVariants(status)
	if _, ok := EPPStatusMap[status]; ok {
		return status
	}
	
	return ""
}

// GetEPPStatusInfo 获取EPP状态信息
func GetEPPStatusInfo(status string) *EPPStatus {
	normalized := NormalizeEPPStatus(status)
	if normalized == "" {
		return nil
	}
	return EPPStatusMap[normalized]
}

// cleanStatusString 清理状态字符串
func cleanStatusString(s string) string {
	// 去除空格
	s = strings.TrimSpace(s)
	if s == "" {
		return ""
	}
	
	// 如果包含 #，取 # 后的部分（ICANN EPP链接格式）
	if i := strings.LastIndex(s, "#"); i >= 0 && i+1 < len(s) {
		s = s[i+1:]
	}
	
	// 取第一个空格前的部分（去除URL等）
	if sp := strings.IndexAny(s, " \t"); sp > 0 {
		s = s[:sp]
	}
	
	// 转小写并去除点号
	s = strings.ToLower(strings.Trim(s, "."))
	
	return s
}

// normalizeVariants 处理状态码变体
func normalizeVariants(s string) string {
	// 处理连字符变体
	variants := map[string]string{
		"auto-renewperiod": "autorenewperiod",
		"auto-renew":       "autorenewperiod",
		"autorenew":        "autorenewperiod",
		"addperiod":        "addperiod",
		"add-period":       "addperiod",
		"renew-period":     "renewperiod",
		"redemption":       "redemptionperiod",
	}
	
	if normalized, ok := variants[s]; ok {
		return normalized
	}
	
	return s
}